import os
import pandas as pd
import numpy as np
from config import config_database


class ServiceEffect(object):
    def __init__(self):
        pass

    # 效果目录列表
    def list_effect(self, **data):
        path = np.array(pd.read_csv(
            config_database['file_source_path']))[0][1]
        path = os.path.join(path, data["packageName"], data["folderName"])
        try:
            array = os.listdir(path)
            print(array)
            data = []
            for item in array:
                if os.path.isfile(os.path.join(path, item)) and item != "Thumbs.db":
                    filename, filetype = os.path.splitext(item)
                    kk = {
                        "title": filename,
                        'gifPath': os.path.join(path, item),
                        "aepPath": os.path.join(path, filename+'.aep')
                    }
                    if filetype == '.gif':
                        data.append(kk)
                    if os.path.exists(kk['gifPath']) is False:
                        return {"errcode": 400, "errmsg": "缺少{filename}.gif文件".format(filename=filename)}
                    if os.path.exists(kk['aepPath']) is False:
                        return {"errcode": 400, "errmsg": "缺少{filename}.aep文件".format(filename=filename)}
            return {"errcode": 200, "errmsg": "ok", "data": data}
        except:
            return {"errcode": 400, "errmsg": "路径错误，导致读取文件失败", "data": []}
